Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow actions to be forked for testing #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

jgaskins
Copy link
Member

@jgaskins jgaskins commented Mar 20, 2018

This lets you fork an action in testing so you don't overwrite its
actual store for integration tests. Here is a simple example:

Action = GrandCentral::Action.create
SetName = Action.with_attributes(:name)
AppState = GrandCentral::Model.with_attributes(:name)

initial_state = AppState.new(name: nil)
Store = GrandCentral::Store.new(initial_state) do |state, action|
  case action
  when SetName
    state.update name: action.name
  else state
  end
end

RSpec.describe SetName do
  let(:store) { Store.dup }
  let(:klass) { SetName.fork }
  before { klass.store = store }

  it 'sets the name' do
    klass.call 'My Name'

    expect(store.state.name).to eq 'My Name'
  end
end

Since the forked action will still match the same as the original in a case statement, it still gets picked up in the production store's state-reducer block.

This lets you fork an action in testing so you don't overwrite its
actual store for integration tests. Here is a simple example:

    Action = GrandCentral::Action.create
    SetName = Action.with_attributes(:name)

    RSpec.describe SetName do
      let(:store) { Store.dup }
      let(:klass) { SetName.fork }
      before { klass.store = store }

      it 'sets the name' do
        klass.call 'My Name'

        expect(store.state.name).to eq 'My Name'
      end
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant